package com.zf.yichat.api.controller.user;

import com.alibaba.fastjson.JSON;
import com.zf.yichat.api.controller.BaseController;
import com.zf.yichat.api.dto.request.UserSignRequest;
import com.zf.yichat.service.UserService;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 签到积分
 *
 * @author fengsong
 * @date create in 10:36 2019/8/8 2019
 */
@RestController
public class UserSignController extends BaseController {


    @Autowired
    private UserService userService;




    /**
     * 签到接口
     */
    @PostMapping("user/sign")
    public FsResponse sign(@RequestBody UserSignRequest params) {

        Long userId = getUserId();
        if(Optional.ofNullable(params.getSignType()).orElse(0) == 1){
           if (!userService.realValidStatus(userId)) {

               return FsResponseGen.failMsg("请先实名认证");
           }
//           User userCheck=userService.selectById(getUserId());
//           if(userCheck.getIdNumber()!=null&&userCheck.getIdNumber().length()>17){
//               Example exampleUserBank=new Example(UserBank.class);
//               exampleUserBank.createCriteria().andEqualTo("userId",getUserId());
//               int count = userBankMapper.selectCountByExample(exampleUserBank);
//               if(count<1){
//
//                   return FsResponseGen.failMsg("请先绑卡");
//               }
//           }


       }



       if(params.getSignType()==1){

           return userService.recordSignForAll(userId, Optional.ofNullable(params.getSignType()).orElse(0) == 1);
       }else {
           return userService.recordSign(userId, Optional.ofNullable(params.getSignType()).orElse(0) == 1);
       }

//       Integer adminType = 2;
//
//        // 更新余额
//       if (adminType == 0) {
//           return userService.recordSign(userId, Optional.ofNullable(params.getSignType()).orElse(0) == 1);
//       }
//
//       // 更新积分
//       if (adminType == 1) {
//           return userService.recordSignForIntegral(userId, Optional.ofNullable(params.getSignType()).orElse(0) == 1);
//       }

       // 两者都更新
      // return userService.recordSignForAll(userId, Optional.ofNullable(params.getSignType()).orElse(0) == 1);
    }

    /**
     * 签到接口-同时新增余额和积分
     */
    @PostMapping("user/signForAll")
    public FsResponse signForAll(@RequestBody UserSignRequest params) {

        Long userId = getUserId();
        if(Optional.ofNullable(params.getSignType()).orElse(0) == 1){
            if (!userService.realValidStatus(userId)) {

                return FsResponseGen.failMsg("请先实名认证");
            }
        }

        // 两者都更新
        return userService.recordSignForAll(userId, Optional.ofNullable(params.getSignType()).orElse(0) == 1);
    }


    /**
     * 签到接口-赠送积分
     */
    @PostMapping("user/signForIntegral")
    public FsResponse signForIntegral(@RequestBody UserSignRequest params) {
        Long userId = getUserId();
       if(Optional.ofNullable(params.getSignType()).orElse(0) == 1){
           if (!userService.realValidStatus(userId)) {

               return FsResponseGen.failMsg("请先实名认证");
           }
       }

        return userService.recordSignForIntegral(userId, Optional.ofNullable(params.getSignType()).orElse(0) == 1);
    }

    /**
     * 显示连续天数-签到接口-赠送积分和余额
     */
    @PostMapping("user/continuousSignForAll")
    public FsResponse continuousSignForAll(@RequestBody UserSignRequest params) {
        Long userId = getUserId();
       if(Optional.ofNullable(params.getSignType()).orElse(0) == 1){
           if (!userService.realValidStatus(userId)) {

               return FsResponseGen.failMsg("请先实名认证");
           }
       }

        return userService.continuousSign(userId, Optional.ofNullable(params.getSignType()).orElse(0) == 1, "0");
    }

    /**
     * 显示连续天数-签到接口-赠送余额
     */
    @PostMapping("user/continuousSignForBalance")
    public FsResponse continuousSignForBalance(@RequestBody UserSignRequest params) {
        Long userId = getUserId();
       if(Optional.ofNullable(params.getSignType()).orElse(0) == 1){
           if (!userService.realValidStatus(userId)) {

               return FsResponseGen.failMsg("请先实名认证");
           }
       }
        return userService.continuousSign(userId, Optional.ofNullable(params.getSignType()).orElse(0) == 1, "1");
    }

    /**
     * 显示连续天数-签到接口-赠送积分
     */
    @PostMapping("user/continuousSignForIntegral")
    public FsResponse continuousSignForIntegral(@RequestBody UserSignRequest params) {
        Long userId = getUserId();
       if(Optional.ofNullable(params.getSignType()).orElse(0) == 1){
           if (!userService.realValidStatus(userId)) {

               return FsResponseGen.failMsg("请先实名认证");
           }
       }

        return userService.continuousSign(userId, Optional.ofNullable(params.getSignType()).orElse(0) == 1, "2");
    }

}
